package yfdc.douyin.download

import android.annotation.SuppressLint
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.KeyEvent
import android.view.View
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import yfdc.douyin.download.dialog.CaptchaDialog
import yfdc.douyin.download.network.WorkFlow

class MainAct : AppCompatActivity(), View.OnClickListener,
        WorkFlow.CaptchaCallback{
    private var exitTime: Long = 0L;
    private var input:EditText? = null
    private var output:TextView? = null
    private var captchaDialog:CaptchaDialog? = null;
    private var workFlow:WorkFlow = WorkFlow().setCallBack(this);
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        input = findViewById(R.id.txt_share_string)
        output = findViewById(R.id.result)
        captchaDialog = CaptchaDialog(this)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == 4 || keyCode == 111) {
            if (System.currentTimeMillis() - exitTime > 2000L) {
                exitTime = System.currentTimeMillis()
                Toast.makeText(this, R.string.str_toast_exit, Toast.LENGTH_SHORT).show()
            } else {
                finish()
            }
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == 4 || keyCode == 111) {
            return true
        }
        return super.onKeyUp(keyCode, event)
    }

    @SuppressLint("all")
    override fun onClick(v: View) {
        val id:Int = v.id
        if (id == R.id.id_btn_decode){
            val input = this.input;
            if (input !== null) {
                val url = input.text.toString()
                if (url.indexOf("https://") >= 0){
                    val start = url.indexOf("https://")
                    val end:Int
                    val tryParse = url.substring(start + 8)
                    end = if(tryParse.startsWith("v.douyin.com")){
                        start + 28;
                    } else {
                        start + "https://www.iesdouyin.com/share/video/7187771634558831911/?".length
                    }
                    workFlow.parseUrl(url.substring(start, end))
                } else {
                    output?.text = "请输入抖音下载URL"
                }
            }
        } else if (id == R.id.id_btn_download){

        } else if (id == R.id.id_btn_clear){
            input?.setText("")
            output?.text = "";
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        kotlin.system.exitProcess(0)
    }

    override fun onCaptchaResult(suc: Boolean, go: String) {
        if (suc){
            Log.d("ACT", go)
        } else {
            NetWork.postOnMainThread() {
                captchaDialog?.showUrl(go)
            }
        }
    }
}
